package vn.aptech.leaveManagementSystem.view.admin;

import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JLabel;
import javax.swing.JComboBox;
import javax.swing.JTextField;
import javax.swing.JButton;

import vn.aptech.leaveManagementSystem.dao.DBManager;

import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

public class DeleteUser extends JFrame{

	DBManager con = new DBManager();
	Connection conn;
	String sql;
	ResultSet rs;
	PreparedStatement ps;
	
	JPanel pnlMain;
	private JTextField txtName;
	JFrame thisFrame = this;
	public DeleteUser() {
		// TODO Auto-generated constructor stub
		
		pnlMain = new JPanel();
		pnlMain.setLayout(null);



		getContentPane().add(pnlMain);
		
		JLabel lblId = new JLabel("ID :");
		lblId.setBounds(25, 49, 46, 14);
		pnlMain.add(lblId);
		
		JLabel lblName = new JLabel("Full Name :");
		lblName.setBounds(25, 77, 89, 14);
		pnlMain.add(lblName);
		
		txtName = new JTextField();
		txtName.setBounds(103, 74, 242, 20);
		pnlMain.add(txtName);
		txtName.setColumns(10);
		
		final JComboBox cbbID = new JComboBox();
		cbbID.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				 sql = "select * from [Employee] where IDEmp = ?";
			        try
			        {
			            conn = con.getConn();
			            PreparedStatement ps = conn.prepareStatement(sql);
			            ps.setString(1,cbbID.getSelectedItem().toString());
			            ResultSet rs = ps.executeQuery();
			            while(rs.next())
			            {
			                txtName.setText(rs.getString(3) + " " + rs.getString(2));
			            }
			            
			            rs.close();
			            conn.close();
			        } 
			        catch (SQLException ex) {
			            ex.printStackTrace();
			        }
			}
		});
		cbbID.setBounds(103, 46, 122, 20);
		pnlMain.add(cbbID);
		
	
		
		
		//get Employee details
				sql = "select * from Employee";
				Statement st;
				try {
					conn = con.getConn();
					st = conn.createStatement();
					rs = st.executeQuery(sql);
					while (rs.next()) {
						cbbID.addItem(rs.getString(1));
					}
					rs.close();
					conn.close();
				} catch (SQLException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
		
		
		JButton btnDelete = new JButton("DELETE");
		btnDelete.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				sql = "delete from [Employee] where IDEmp = ?";
		        
		        try
		        {
		        	conn = con.getConn();
		            PreparedStatement ps = conn.prepareStatement(sql);
		            ps.setString(1,cbbID.getSelectedItem().toString());
		            
		            int row = ps.executeUpdate();
		            if(row!=0)
		            {
		                JOptionPane.showMessageDialog(thisFrame,"Delete successful");               
		                setVisible(false);
		                
		            }
		            else
		            {
		                JOptionPane.showMessageDialog(thisFrame,"Delete fail");
		            }
		        }
		        catch (SQLException ex) {
		            ex.printStackTrace();
		        }
			}
		});
		btnDelete.setBounds(99, 131, 89, 23);
		pnlMain.add(btnDelete);
		
		JButton btnCancel = new JButton("CANCEL");
		btnCancel.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				setVisible(false);
				AdminForm adminForm = new AdminForm();
				adminForm.setVisible(true);
			}
		});
		btnCancel.setBounds(198, 131, 89, 23);
		pnlMain.add(btnCancel);
		setVisible(true);
		setTitle("Delete User");
		setSize(390, 569);
		setLocationRelativeTo(null);
	}
}
